﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using iOSClub.Toolkit.WindowApp.Pages;
using iOSClub.Toolkit.WindowApp.ViewModels.Pages;
using Toolkit.Lib.iOSModels;

namespace iOSClub.Toolkit.WindowApp.MemberControls;

public partial class CentreControl : UserControl
{
    public CentreControl()
    {
        InitializeComponent();
    }

    protected override async void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        var model = ViewServes.GetViewData<iOSManagerPageModel>(this);
        if (model == null) return;
        var result = await model.GetMemberData();
        if (result == null) return;
        NameBox.Text = result.UserName;
        IdBox.Text = result.UserId;
        AcademyBox.ItemsSource = SignRecord.Academies;
        AcademyBox.SelectedItem = result.Academy;
        LandscapeBox.ItemsSource = SignRecord.PoliticalLandscapes;
        LandscapeBox.SelectedItem = result.PoliticalLandscape;
        GenderBox.ItemsSource = SignRecord.Genders;
        GenderBox.SelectedItem = result.Gender;
        ClassBox.Text = result.ClassName;
        PhoneNumBox.Text = result.PhoneNum;
    }

    private async void ChangeClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(NameBox.Text) || string.IsNullOrEmpty(IdBox.Text) ||
            AcademyBox.SelectedItem == null || LandscapeBox.SelectedItem == null || GenderBox.SelectedItem == null ||
            string.IsNullOrEmpty(ClassBox.Text) || string.IsNullOrEmpty(PhoneNumBox.Text))
        {
            var close = new TaskDialog
            {
                Title = "西建大iOS Club工具包",
                Header = "数据更新",
                Content = "未填完值",
                IconSource = new SymbolIconSource { Symbol = Symbol.People },
                FooterVisibility = TaskDialogFooterVisibility.Never,
                XamlRoot = (Visual)VisualRoot!,
                Buttons =
                {
                    TaskDialogButton.OKButton
                }
            };

            await close.ShowAsync();
            return;
        }

        var result = new SignModel
        {
            UserId = IdBox.Text,
            UserName = NameBox.Text,
            Academy = AcademyBox.SelectedItem.ToString()!,
            ClassName = ClassBox.Text,
            Gender = GenderBox.SelectedItem.ToString()!,
            PoliticalLandscape = LandscapeBox.SelectedItem.ToString()!,
            PhoneNum = PhoneNumBox.Text
        };
        
        var td = new TaskDialog
        {
            Title = "西建大iOS Club工具包",
            Header = "数据更新",
            Content = "确定更改?",
            IconSource = new SymbolIconSource { Symbol = Symbol.People },
            FooterVisibility = TaskDialogFooterVisibility.Never,
            XamlRoot = (Visual)VisualRoot!,
            Buttons =
            {
                TaskDialogButton.OKButton,
                TaskDialogButton.CancelButton
            }
        };

        td.Closing += async (dialog, args) =>
        {
            if ((TaskDialogStandardResult)args.Result != TaskDialogStandardResult.OK) return;
            var model = ViewServes.GetViewData<iOSManagerPageModel>(this);
            if (model == null) return;
            _ = await model.Update(result);
        };
    }
}